<!--* Created by caiqi on 16/12/19.-->
<template>
  <div class="docu">
    <h2 class="title">{{detailData.title}}</h2>
    <div class="writer">作者：{{detailData.writer}}</div>
    <div class="content" v-html="detailData.content" v-highlight></div>
    <el-button
      class="like"
      :icon="like?'el-icon-star-on':'el-icon-star-off'"
      circle
      @click="toLike"
    ></el-button>
  </div>
</template>

<script>
import api from "@/api/index";
// import tools from "@/server/storage.server";

export default {
  components: {},
  data() {
    return {
      like: false,
      readWatch: {},
      detailId: this.$route.params.id,
      detailData: {}
    };
  },
  methods: {
    // ========================================  获取文章列表
    async getDocDetails() {
      const { data } = await api.getDocDetails(this.detailId);
      this.detailData = data;
    },
    // ========================================  设置阅读数
    async setRed() {
      const { code } = await api.getDocRead(this.detailId);
      if (code === 0) {
        this.$message.success("如果对你有用就在下方点个星星吧~");
      }
    },
    // ========================================  设置喜欢数
    async toLike() {
      if (this.like) {
        return;
      }
      const { code } = await api.getDocLike(this.detailId);
      if (code === 0) {
        this.like = true;
        this.$message.success("感谢你喜欢这篇内容~");
      }
    }
  },
  computed: {},
  mounted() {
    this.getDocDetails();
    this.readWatch = setTimeout(() => {
      this.setRed();
    }, 5000);
  },

  watch: {},
  beforeRouteLeave: function(to, from, next) {
    clearTimeout(this.readWatch);
    next();
  }
};
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
.docu {
  padding: 25px 0;
  .title {
    color: #535050;
    text-align: center;
  }
  .writer {
    margin-top: 12px;
    text-align: center;
    font-size: 16px;
    color: #909399;
  }
  .content {
    padding: 20px;
    padding-bottom: 0;
    font-size: 16px;
    line-height: 1.5em;
  }
  .like {
    display: block;
    margin: 0 auto;
  }
}
.el-button {
}
</style>